package sample;

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class SnowFlow extends Application {
    private static String path ;
    private  double time ;
    private String url;
    private String buttonName;
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();
        Button button = new Button("雪花");
        Button button1 = new Button("雨珠");
        hBox.getChildren().addAll(button,button1);
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: plum");
        AnchorPane.setBottomAnchor(hBox,100.0);
        anchorPane.getChildren().add(hBox);
        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(Screen.getPrimary().getBounds().getWidth());
        primaryStage.setHeight(Screen.getPrimary().getBounds().getHeight());
        primaryStage.setFullScreen(true);
        primaryStage.show();


        button.setOnAction((event -> {
            url = path+"image/雪花.png";
            buttonName = "雪花";
            initSnow(primaryStage.getWidth(),primaryStage.getHeight()-200,anchorPane);
        }));
        button1.setOnAction((event -> {
            buttonName = "水滴";
            url = path+"image/水滴.png";
            initSnow(primaryStage.getWidth(),primaryStage.getHeight()-200,anchorPane);
        }));

    }

    public void initSnow(double w ,double h , AnchorPane anchorPane){
        Random random = new Random();
//        Random random1 = new Random(100);
        List<ImageView> imageViewList =  new ArrayList<>();
        for (int i = 0; i < 100 ; i++) {
            int x = random.nextInt(2000);
            int y = random.nextInt(300);
            int z = random.nextInt(2000);
            if(random.nextBoolean()) {
                x += 100;
                z += 200;
            }else
                x -= 100;
            ImageView imageView = customImageView(url);
            imageView.setTranslateY(y);
            imageView.setTranslateX(x);
            imageView.setTranslateZ(z);
            imageViewList.add(imageView);
        }
        PerspectiveCamera perspectiveCamera = new PerspectiveCamera();
        AnchorPane pane = new AnchorPane();
        pane.getChildren().addAll(imageViewList);
        SubScene subScene = new SubScene(pane,w,h,false, SceneAntialiasing.BALANCED);
        subScene.setCamera(perspectiveCamera);
        SnowAnimation(pane,imageViewList);
        AnchorPane.setBottomAnchor(subScene,200.0);
        anchorPane.getChildren().add(subScene);

    }


    public void SnowAnimation(AnchorPane anchorPane,List<ImageView> viewList){

        viewList.forEach((imageView -> {
            time = new Random().nextDouble()*5+5;
            //旋转动画
            RotateTransition rotateTransitio = new RotateTransition();
            if(!buttonName.equals("水滴")) { 
                rotateTransitio.setDuration(Duration.seconds(time));
                rotateTransitio.setFromAngle(0);
                rotateTransitio.setToAngle(360);
            }

            //消失动画
            FadeTransition fadeTransition = new FadeTransition(Duration.seconds(time/2));
            fadeTransition.setFromValue(0);
            fadeTransition.setToValue(1);
            FadeTransition fadeTransition1 = new FadeTransition(Duration.seconds(time/2));
            fadeTransition1.setFromValue(1);
            fadeTransition1.setToValue(0);


            //串行动画
            SequentialTransition sequentialTransition = new SequentialTransition();
            sequentialTransition.getChildren().addAll(fadeTransition,fadeTransition1);
            //位移动画
            TranslateTransition translateTransition = new TranslateTransition();
            translateTransition.setDuration(Duration.seconds(time));
            translateTransition.setFromY(imageView.getTranslateY());
            translateTransition.setToY(1300);

            ParallelTransition parallelTransition = new ParallelTransition();
            parallelTransition.setNode(imageView);
            parallelTransition.getChildren().addAll(rotateTransitio,sequentialTransition,translateTransition);
            parallelTransition.setCycleCount(Animation.INDEFINITE);
            parallelTransition.play();
        }));

    }

    public static void main(String[] args) {
       path = new SnowFlow().getHostServices().getDocumentBase();
       launch(args);
    }
    public ImageView customImageView(String url){
        ImageView imageView = new ImageView(url);
        return  imageView;
    }
}
